package com.role.entities
{
	import com.role.embeds.Assets;
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPEntity;
	import com.saia.starlingPunk.utils.*;
	
	import flash.display.Bitmap;
	
	import starling.animation.IAnimatable;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.events.*;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;

	public class PlayerEntity extends SPEntity
	{
		private var _daiji_role:MovieClip;
		private var _pao_role:MovieClip;
		
		private var _curRole:MovieClip;
		private var _curScaleX:int = 1;
		
		private var _isChanged:Boolean = false;
		
		private var _curState:String;
		public function get curState() : String
		{
			return this._curState;
		}
		
		public const PAO:String = 'pao_';
		public const DAIJI:String = 'daiji_';
		
		public function PlayerEntity()
		{
			type = "player";
		}
		
		override public function added() : void
		{
			super.added();
			
			init();
			
			State = DAIJI;
		}
		
		override public function removed() : void
		{
			Starling.juggler.remove(_daiji_role);
		}
		
		override public function update() : void
		{
			super.update();
			handleInput();
			handleState();
		}
		
		public function set State(state:String) : void
		{
			switch(state)
			{
				case DAIJI:
				case PAO:
					if (_curState != state)
					{
						_curState = state;
						_isChanged = true;
					}
					break;
				default:
					throw new Error("没有匹配的状态！");
			}
		}
		
		
		///
		///	private method
		///
		
		private function init() : void
		{
			_daiji_role = getMovieClip(DAIJI, 15);
			_pao_role = getMovieClip(PAO, 15);
		}
		
		
		private function get textureAtlas() : TextureAtlas
		{
			var bitmap:Bitmap = new Assets.ROLE_TEXTURE();
			var texture:Texture = Texture.fromBitmap(bitmap);			
			var xml:XML = new XML(new Assets.ROLE_XML());
			
			var sTextureAtlas:TextureAtlas = new TextureAtlas(texture, xml);
			
			return sTextureAtlas;
		}
		
		private function getMovieClip(name:String, fps:Number) : MovieClip
		{
			var mp:MovieClip;
			
			var frames:Vector.<Texture> = textureAtlas.getTextures(name);
			
			mp = new MovieClip(frames, fps);
			
			mp.pivotX = mp.width/2;
			mp.pivotY = mp.height/2;
			
			mp.x = SP.width - mp.width >> 1;
			mp.y = SP.height - mp.height >> 1;
			
			return mp;
		}
		
		private function handleInput() : void
		{
			if (SPInput.check(Key.LEFT))
			{
				_curScaleX = -1;
			}
			
			if (SPInput.check(Key.RIGHT))
			{
				_curScaleX = 1;
			}
			
			if (SPInput.check(Key.SPACE))
			{
				State = DAIJI;
			}
			
			if (SPInput.check(Key.ENTER))
			{
				State = PAO;
			}
		}
		
		private function handleState() : void
		{
			if (!_isChanged) return;
			
			removeFromStage(_curRole);
			
			switch(_curState)
			{
				case DAIJI:				
					addToStage(_daiji_role);
					_curRole = _daiji_role;
					break;
				case PAO:
					addToStage(_pao_role);
					_curRole = _pao_role;
					break;
				default:
					throw new Error("没有匹配的状态！");
			}
			
			_curRole.scaleX = _curScaleX;
		}
		
		private function removeFromStage(obj:DisplayObject) : void
		{
			if (!obj) return;
			
			if (obj.parent) obj.parent.removeChild(obj);
			
			if (obj is IAnimatable) Starling.juggler.remove(obj as IAnimatable);
		}
		
		private function addToStage(obj:DisplayObject) : void
		{
			if (!obj) return;
			
			if (!obj.parent) addChild(obj);
			
			if (obj is IAnimatable) Starling.juggler.add(obj as IAnimatable);
		}
	}
}